import { axios, defaultAPI } from '../../utils/http'
Component({
  options: {
    multipleSlots: true //这样就可以设置多个slot插槽
  },
  properties: {
    pdfProperty: {
      type: Object,
      value: {}
    }
  },
  data: {
    progress: 0,
    showProgress: false
  },
  methods: {
    downLoadPdf(e) {
      axios.get('/util/valid').then(res => {
        if (res.data.data) {
          this.setData({
            showProgress: true
          })
          const url = e.currentTarget.dataset['prop']
          let task = wx.downloadFile({
            url: defaultAPI + '/pdf/download?id=' + url,
            success: res => {
              wx.openDocument({
                filePath: res.tempFilePath,
                showMenu: true,
                success: res => {
                  wx.showToast({
                    title: '下载成功',
                    icon: 'success'
                  })
                }
              })
            },
            complete: res => {
              this.setData({
                showProgress: false
              })
            },
            fail: res => {
              wx.showToast({
                title: '下载失败',
                icon: 'error'
              })
            }
          })
          task.onProgressUpdate(e => {
            this.setData({
              progress: e.progress
            })
          })
        } else {
          wx.showToast({
            title: '登陆过期',
            icon: 'error'
          })
        }
      })
    }
  }
})
